package panda.query.scan;

import panda.query.condition.BooleanExpression;
import panda.query.struct.Attribute;
import panda.query.struct.Constant;
import panda.record.TupleToken;
import panda.transaction.Transaction;

/**
 * 
 * @author Tian Yuan
 *
 */
public class SelectScanner implements UpdatableScanner{
	
	private Scanner s;
	private BooleanExpression c;
	private Transaction tx;
	
	/**
	 * 
	 * @param s
	 * @param c
	 */
	public SelectScanner(Scanner s, BooleanExpression c, Transaction tx) {
		this.s = s;
		this.c = c;
		this.tx = tx;
	}
	
	/**
	 * 
	 */
	public void init() {
		s.init();
	}

	/**
	 * 
	 */
	public void close() {
		s.close();
	}

	/**
	 * 
	 */
	public boolean next() {
		while (s.next())
			if (c.isSatisfied(s, tx) > 0) return true;
		return false;
	}

	/**
	 * 
	 */
	public Constant getValue(Attribute attr) {
		return s.getValue(attr);
	}

	/**
	 * 
	 */
	public boolean hasAttribute(Attribute name) {
		return s.hasAttribute(name);
	}

	@Override
	public void insert() {
		UpdatableScanner us = (UpdatableScanner)s;
		
		us.insert();
	}
	
	@Override
	public void delete() {
		UpdatableScanner us = (UpdatableScanner)s;
		
		us.delete();
	}

	@Override
	public TupleToken getCursor() {
		UpdatableScanner us = (UpdatableScanner)s;
		return us.getCursor();
	}


	@Override
	public void setCursor(TupleToken t) {
		UpdatableScanner us = (UpdatableScanner)s;
		
		us.setCursor(t);		
	}

	@Override
	public void setValue(Attribute attr, Constant value) {
		UpdatableScanner us = (UpdatableScanner)s;
		
		us.setValue(attr, value);
	}

}
